﻿using EYangFrame.DataAccess.Global;
using EYangFramework.Common.Ninject;
using EYangFramework.CommonForm;
using EYangFramework.Domain.Entitys.Entrys;
using EYangFramework.Domain.Entitys.ValueObjects;
using EYangFramework.Domain.Repositorys;
using SMES.Common;
using System;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using EYangFramework.MessageSystem;

namespace EyangFrame.Main
{
    public partial class frm_Main : Form
    {
        IRepository<User> RepositoryUser = NinjectProvider.NinjectCore.GetService<IRepository<User>>();
        IRepository<Navigation> RepositoryNavigation = NinjectProvider.NinjectCore.GetService<IRepository<Navigation>>();
        public frm_Main()
        {
            InitializeComponent();
        }

        private void txtUserName_Click(object sender, EventArgs e)
        {
            txtUserName.Text = "";
        }

        private void txtUserName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtPassword.Focus();
            }
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin_Click(null, null);
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            var pwd = txtPassword.Text.Trim();
            var uid = txtUserName.Text.Trim();
            if (string.IsNullOrEmpty(pwd) || string.IsNullOrEmpty(uid))
            {
                MessageBox.Show("用户名或密码不能为空,请填写完整!");
                return;
            }
            var enPwd = CEncoder.encryptMD5(pwd);
            var currentUser = RepositoryUser.GetAllList(t => (t.USERNAME == uid || t.EMAIL == uid) && !t.ISDELETE).FirstOrDefault();
            if (currentUser == null)
            {
                MessageBox.Show("不存在当前用户,请重新输入!");
                return;
            }
            if (currentUser.PASSWORD != enPwd)
            {
                MessageBox.Show("密码错误,请重新输入!");
                return;
            }
            //使用异步锁实现一个用户只能在一台电脑上登录一次
            bool isExist = false;
            Mutex mutex = new Mutex(false, currentUser.USERNAME, out isExist);
            if (!isExist && !GlobalInfo.IsLogin)
            {
                MessageBox.Show("当前用户已在此电脑登录,请直接切换到对应程序!");
                Application.Exit();
                return;
            }
            GlobalInfo.CurrentUser = currentUser;
            if (!currentUser.CHANGEPASSWORD)
            {
                frm_ModifyPassword frmModifyPassword = new frm_ModifyPassword();
                frmModifyPassword.ShowDialog();
            }
            txtPassword.Text = "";
            GlobalInfo.IsLogin = true;
            frm_ContainerBase frm_Container = NinjectProvider.NinjectCore.GetService<frm_ContainerBase>();
            frm_Container.Tag = this;
            frm_Container.Show();
            this.Hide();
            Form frm_MessageMain = NinjectProvider.NinjectCore.GetService<frm_MessageMain>();
            frm_MessageMain.Show();
        }

        private void frm_Main_FormClosed(object sender, FormClosedEventArgs e)
        {
            GlobalInfo.IsClosed = true;
            Application.Exit();//当主窗体被关闭的时候,进程也被关闭
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你确定需要退出程序吗?", "警告!", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                GlobalInfo.IsClosed = true;
                Application.Exit();
            }
        }
    }
}
